package com.example.workapp;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

public class WebActivity extends AppCompatActivity {
    private WebView mWebView;
    private  final int hand_js_doJsFunction = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        IniteWebView();
    }
    //绑定webview配置及加载本地页面
    private void IniteWebView(){
        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                // 打印控制台输出
                // 你可以根据需要将消息发送到 Logcat，或者显示在 UI 上
                String message = consoleMessage.message() + " -- From line " + consoleMessage.lineNumber();
                System.out.println(message); // 或者使用 Log.d
                return super.onConsoleMessage(consoleMessage);
            }
        });
        WebSettings settings = mWebView.getSettings();
        //允许js交互
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setAllowContentAccess(true);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        settings.setBlockNetworkLoads(false);

        Intent currentIntent=getIntent();
        String filename=currentIntent.getStringExtra("filename"),username=currentIntent.getStringExtra("username");

        mWebView.loadUrl( "file:///android_asset/collaborative_office.html?filename="+filename+"&username="+username);//本地文件
        //mWebView.loadUrl("https://www.baidu.com");//网络文件

        //定义给页面js调用app的方法
        //mWebView.addJavascriptInterface(appToJsObject, "AndroidJS");

    }


    //== webview 与js交互=========================
    //定义提供html页面调用的方法
    public final Object appToJsObject = new Object() {

        @JavascriptInterface
        public void GetAppInfo() {//获取app信息
            String appInfo =  getAppInfo();

            doJsFunction("backInfo('"+appInfo+"')");
        }
    };
    //定义公共方法调用页面js方法
    public void doJsFunction(String _url)
    {

        Message msg = new Message();
        msg.what = hand_js_doJsFunction;
        Bundle bundle = new Bundle();
        bundle.putString("url",_url);  //往Bundle中存放数据
        msg.setData(bundle);//mes利用Bundle传递数据
        handler.sendMessage(msg);//用activity中的handler发送消息

    }
    //用handler访问让方法在主进程内处理
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            String[] args = (String[]) msg.obj;

            switch (msg.what) {

                case hand_js_doJsFunction:
                    // String str1 = msg.getData().getString("url");//接受msg传递过来的参数
                    //调用页面js方法
                    mWebView.loadUrl("javascript:"+msg.getData().getString("url"));
                    break;

                default:
                    break;

            }

        }
    };

    //获取应用信息
    public  String getAppInfo() {
        String info = "";
        try {
            Context mContext = getApplicationContext();
            PackageManager packageManager = mContext.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);
            info += "版本号：" + packageInfo.versionName + "\n";
            info += "包  名：" + packageInfo.packageName + "\n";


        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

}